package oa.hleast.xswl.service.pms.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.enums.CouponSourceEnum;
import oa.hleast.xswl.mapper.pms.PmsUserCouponMapper;
import oa.hleast.xswl.pojo.domain.pms.PmsCoupon;
import oa.hleast.xswl.pojo.domain.pms.PmsUserCoupon;
import oa.hleast.xswl.pojo.dto.CommonResponse;
import oa.hleast.xswl.service.pms.PmsCouponService;
import oa.hleast.xswl.service.pms.PmsUserCouponService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: 00209
 * @date: 2021-06-10 17:11:34
 * @description: 用户优惠券表(PmsUserCoupon)接口实现
 */
@Transactional
@AllArgsConstructor
@Service("pmsUserCouponService")
public class PmsUserCouponServiceImpl extends ServiceImpl<PmsUserCouponMapper, PmsUserCoupon> implements PmsUserCouponService {

    private PmsCouponService pmsCouponService;

    @Override
    public Boolean receive(Long id, Long userId, CouponSourceEnum couponSourceEnum) {

        PmsCoupon pmsCoupon = pmsCouponService.getById(id);

        //todo: 优惠券可领取

        // 领取优惠券
        PmsUserCoupon pmsUserCoupon = new PmsUserCoupon();
        BeanUtil.copyProperties(pmsCoupon, pmsUserCoupon);
        pmsUserCoupon.setCouponId(id);
        pmsUserCoupon.setUserId(userId);
        pmsUserCoupon.setSource(couponSourceEnum.getCode());
        this.save(pmsUserCoupon);

        // 优惠券发行数量-1, 领取数量+1
        pmsCouponService.update(new LambdaUpdateWrapper<PmsCoupon>()
                .setSql("publish_nums = publish_nums - 1")
                .setSql("receive_nums = receive_nums + 1")
                .eq(PmsCoupon::getId, id).gt(PmsCoupon::getPublishNums, 0));
        return Boolean.TRUE;

    }

    @Override
    public List<PmsUserCoupon> bestList(Long totalPrice) {
        Long userId = RequestUtils.getUserId();
        LambdaQueryWrapper<PmsUserCoupon> queryWrapper = new LambdaQueryWrapper<PmsUserCoupon>()
                .le(PmsUserCoupon::getLimitPrice, totalPrice)
                .isNull(PmsUserCoupon::getUseTime)
                .eq(PmsUserCoupon::getUserId, userId)
                .ge(PmsUserCoupon::getUseEnd, new Date())
                .le(PmsUserCoupon::getUseStart, new Date())
                .orderByDesc(PmsUserCoupon::getPrice);

        List<PmsUserCoupon> userCoupons = this.list(queryWrapper);

        return userCoupons;
    }

    @Override
    public PmsUserCoupon getByUserCouponId(Long userCouponId) {
        Long userId = RequestUtils.getUserId();

        LambdaQueryWrapper<PmsUserCoupon> queryWrapper = new LambdaQueryWrapper<PmsUserCoupon>()
                .eq(PmsUserCoupon::getUserId, userId)
                .eq(PmsUserCoupon::getId, userCouponId);

        List<PmsUserCoupon> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list) && ObjectUtil.isNotNull(list.get(0)) ){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void useUserCoupon(Long userCouponId) {

    }
}
